﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Xml.Linq;
using System.IO;

namespace web.component
{
    public class Uxmlhelper
    {
        /// <summary>
        /// tao file xml tu Dictionary obj
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="config_file"></param>
        public static void CreateTranslateFile(Dictionary<string, string> dict, string pageLang, string config_file)
        {
            try
            {
                var xmlDoc = new XDocument(new XDeclaration("1.0", "utf-8", "true"),
                                                       new XComment(
                                                           string.Format("XML language file created by tool at {0}", Udate_time.GetCurrentDateTime())));
                //root of document
                var root = new XElement("root",
                                                    new XAttribute("page_lang", pageLang),
                                                    new XComment("page content"));
                //var appSettings = new XElement("appSettings");
                foreach (var item in dict)
                {
                    if (!string.IsNullOrEmpty(item.Key))
                    {
                        root.Add(new XElement("item",
                                    new XAttribute("key", item.Key),
                                    new XElement("value", item.Value)
                        ));
                    }
                }
                xmlDoc.Add(root);

                //create folder if not exist
                var pathStore = Path.GetDirectoryName(config_file);
                if (!Directory.Exists(pathStore))
                    Directory.CreateDirectory(pathStore);

                // Saving to a file, you can also save to streams
                xmlDoc.Save(config_file);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //get node in translate xml file
        public static string GetNodeTrans(string key, string path)
        {
            try
            {
                string str_ret = "";
                XDocument doc = XDocument.Load(path);
                foreach (XElement el in doc.Root.Elements("item"))
                {
                    if (el.Attribute("key").Value == key)
                    {
                        str_ret = el.Element("value").Value;
                        break;
                    }
                }
                return str_ret;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        // get translate text in site
        public static string GetTranslate(global_const.E_TRANS_GROUP transgroup, string key, string lang_code)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(lang_code))
            {
                //get location store file
                string file_name = string.Format(global_const.TRANS_XML_FILE, lang_code, (int)transgroup);
                string path = System.Web.HttpContext.Current.Server.MapPath("/" + global_const.STORAGE_FOLDER) + "\\" + file_name;
                // get content
                string str_ret = GetNodeTrans(key, path);
                if (string.IsNullOrEmpty(str_ret))
                    str_ret = string.Format("[trans_{0}]", key);
                return str_ret;
            }
            return "[trans_no_key_or_lang]";
        }

    }    
}
